﻿using System;
using wojilu.Apps.Qycms.Interface;
using wojilu.Apps.Qycms.Service;
using wojilu.Web.Mvc;

namespace wojilu.Web.Controller.Common
{
    public class BaseController : ControllerBase
    {
        public virtual IConfigsService configsService { get; set; }

        public String DefaultLng
        {
            get
            {
                var Lngs = ctx.web.CookieGet("DefaultLng");
                if (!strUtil.HasText(Lngs))
                {
                    Lngs = YBBCommon.GetConfigStr("default_lng");
                    ctx.web.CookieSet("DefaultLng", Lngs);
                }
                return Lngs;
            }
        }

        public BaseController()
        {
            configsService = new ConfigsService();
        }

        protected void SetPublic()
        {
            set("softtitle", configsService.GetValue("sitename"));
        }

        #region 取左、中、右
        /// <summary>
        /// 取左边的字符
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iLength">要取长度</param>
        /// <returns></returns>
        public String Left(string sSource, int iLength)
        {
            if (!sSource.StrIsNullOrEmpty())
            {
                if (iLength > sSource.Length)
                {
                    return sSource;
                }
                else
                {
                    return sSource.Substring(0, iLength);
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取右边的字符
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iLength">要取长度</param>
        /// <returns></returns>
        public String Right(string sSource, int iLength)
        {
            if (!sSource.StrIsNullOrEmpty())
            {
                if (iLength > sSource.Length)
                {
                    return sSource;
                }
                else
                {
                    return sSource.Substring(sSource.Length - iLength, iLength);
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取中间的字符。
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iStart">开始长度</param>
        /// <param name="iLength">结束长度</param>
        /// <returns></returns>
        public String Mid(string sSource, int iStart, int iLength)
        {
            if (!sSource.StrIsNullOrEmpty())
            {
                int iStartPoint = iStart > sSource.Length ? sSource.Length : iStart;
                return sSource.Substring(iStartPoint, iStartPoint + iLength > sSource.Length ? sSource.Length - iStartPoint : iLength);
            }
            return string.Empty;
        }

        #endregion


    }
}
